package com.ving.test.mybatis01.session;

import com.ving.test.mybatis01.Configuration01;
import com.ving.test.mybatis01.binding.MapperMethod01;
import com.ving.test.mybatis01.binding.MapperProxy01;
import com.ving.test.mybatis01.excutor.SimpleExecutor01;

import java.lang.reflect.Proxy;

/**
 * Created by liuweijie on 2019-11-19.
 */
public class DefaultSqlSeesion01 implements SqlSession01 {
    private Configuration01 configuration01;
    private SimpleExecutor01 executor;

    public DefaultSqlSeesion01(Configuration01 configuration01, SimpleExecutor01 simpleExecutor01) {
        this.configuration01=configuration01;
        this.executor=simpleExecutor01;
    }

    public Configuration01 getConfiguration01() {
        return configuration01;
    }

    public SimpleExecutor01 getSimpleExecutor01() {
        return executor;
    }

    @Override
    public <T> T getMapper(Class<T> zlass) {

        // 使用jdk动态代理，返回给用户是一个代理对象。而jdk动态代理要定义接口才能使用
        return (T) Proxy.newProxyInstance(zlass.getClassLoader(),new Class[]{zlass},new MapperProxy01<>(this,zlass));

    }

    @Override
    public <T> T selectOne(MapperMethod01 mapperMethod01, String parmeter) {

        return executor.query(mapperMethod01,parmeter);
    }
}
